<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\model;


/**
 * 学生锻炼-模型
 * @author 牧羊人
 * @since: 2023/09/02
 * Class Memb
 * @package app\admin\model
 */
class Memb extends MemmidModel
{
    // 设置数据表名
    protected $name = "member";

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     * @return \app\common\model\数据信息|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 牧羊人
     * @since: 2023/09/01
     */
    public function getInfo($id)
    {
        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
        $post = input();
        /*$stu_map = [
            'status' => 1,
            'mark' => 1,
            'superior' => $id,
        ];*/
        $rmap[] = ['memberId','=',$id];
        $rmap[] = ['type','=',2];

        if(getParamInt('gender')){
            $rmap[] = ['gender','=',$post['gender']];
        }

        if(getParamInt('professionalId')){
            $prof = Professional::get(input('professionalId'));
            $info['prof_name']= $prof?$prof['name']:'';
        }else{
            $prof = Professional::get($info['professional']);
            $info['prof_name']= $prof?$prof['name']:'';
        }

        if(getParamInt('classId')){
            $class = Clssg::get(input('classId'));
            $info['class_name']= $class?$class['name']:'';
        }else{
            $class = Clssg::get($info['class']);
            $info['class_name']= $class?$class['name']:'';
        }


        if(getParamInt('departmentsId')){
            $depart = Department::get(input('departmentsId'));
            $info['depart_name']= $depart?$depart['name']:'';
        }else{
            $depart = Department::get($info['superior']);
            $info['depart_name']= $depart?$depart['name']:'';
        }


        if(getParamInt('projectId')){
            $pro = Project::get(input('projectId'));
            $info['pro_name']= $pro?$pro['name']:'';
            $rmap[] = ['projectId','=',input('projectId')];
        }

        if(getParamInt('gradeId')){
            $grade = Grade::get(input('gradeId'));
            $info['grade_name']= $grade?$grade['name']:'';
            /*  $stu_map+=[
                  'grade' => $post['gradeId'],
              ];*/
            $rmap[] = ['gradeId','=', $post['gradeId']];
        }else{
            $grade = Grade::get( $info['grade']);
            $info['grade_name']= $grade?$grade['name']:'';
            //$rmap[] = ['gradeId','=',  $info['grade']];
        }

        $avg = 0;
        if(!empty(getParamStr('startTime')) && getParamStr('endTime')){
            $rmap[] = ['startTime',['>', $post['startTime']],'<', $post['endTime']];
        }
        if(isset($post['hasTest']) && $post['hasTest'] === 0){
            $rmap[] = ['model','=',$post['hasTest']];
        }
        if(isset($post['hasTest']) && $post['hasTest'] == 1){
            $rmap[] = ['model','=',$post['hasTest']];
        }
        if(getParamInt('annual')){
            $ann = Annual::getTimeArr($post['annual']);
            $rmap[] = ['startTime',['>',$ann[0]],['<', $ann[1]]];
        }
        $info['gender_str'] = genderStr($info['gender']);
        //professionalId
        $avg = SportRecord::where($rmap)->avg('pingscore');
        $info['avg'] = (int)$avg;
        $goodNum = self::countRaningNum($rmap,1);
        $liangNum = self::countRaningNum($rmap,2);
        $jigeNum = self::countRaningNum($rmap,3);
        $noJigeNum = self::countRaningNum($rmap,0);
        $total = $goodNum+$liangNum+$jigeNum+$noJigeNum;
        $info['youRate'] = cacl_rate($goodNum,$total).'%';
        $info['hegeRate'] = cacl_rate($jigeNum+$liangNum+$goodNum,$total).'%';


        if ($info) {

        }
        return $info;
    }

    public static function allNum($sex = '')
    {
        $map = [
            'status' => 1,
            'mark' => 1,
        ];
        if(!empty($sex)){
            $map += [
                'gender' => $sex
            ];
        }

        $count = self::where($map)->field('id')->count();
        return $count;
    }

    public static function genderTotal()
    {
        $map = [
            'status' => 1,
            'mark' => 1,
        ];
        $res = self::where($map)->fieldRaw('gender, count(id) total')->group('gender')->select()->toArray();
        return array_column($res, 'total', 'gender');
    }
}